﻿#include "LowLevelTask.h"
#include "TaskThread.h"

LowLevelTask::LowLevelTask() : BaseTask()
{
}

LowLevelTask::~LowLevelTask()
{
}

bool LowLevelTask::run()
{
    long lRet = 0;
    do
    {
        long retx1, rety1, retx2, rety2;

        if (isRoleMoving())
        {
            return false;
        }

        TaskStatus status = receiveTask();
        if (status == TaskStatus_Running)
        {
            wd().Delay(200, 300);
        }

        if (status == TaskStatus_Finished)
        {
            return true;
        }

    } while (0);
    return false;
}

TaskStatus LowLevelTask::receiveTask()
{
    long lRet = 0;
    long retx1, rety1, retx2, rety2;

    // 未完成 任务
    wd().UseDict(L"任务状态", &lRet);
    wd().FindStr(623, 251, 851, 461, L"未完成", 0.95, &retx1, &rety1, &retx2, &rety2, &lRet);
    if (lRet != 0)
    {
        {
            long retx1, rety1, retx2, rety2;
            // 技能任务
            wd().UseDict(L"白色", &lRet);
            wd().FindStr(623, 251, 851, 461, L"技能", 0.98, &retx1, &rety1, &retx2, &rety2, &lRet);
            if (lRet != 0)
            {
                wd().MoveTo(583, 656, &lRet);
                wd().Delay(200);
                wd().LeftClick(&lRet);
                wd().Delay(500);
                wd().UseDict(L"绿色标识", &lRet);
                wd().FindStr(246, 80, 679, 615, L"向上箭头", 0.95, &retx1, &rety1, &retx2, &rety2, &lRet);
                if (lRet != 0)
                {
                    wd().MoveTo(retx1 + 10, rety1, &lRet);
                    wd().Delay(200);
                    wd().RightClick(&lRet);
                    wd().Delay(200);
                    wd().MoveTo(398, 343, &lRet);
                    wd().Delay(200);
                    wd().LeftClick(&lRet);
                    return TaskStatus_Running;
                }
            }
        }
        wd().MoveTo(retx1 - 30, rety1, &lRet);
        wd().Delay(200);
        wd().LeftClick(&lRet);
        wd().Delay(200);
        wd().MoveTo(3, 3, &lRet);

        return TaskStatus_Running;
    }
    // 已完成 任务
    wd().UseDict(L"任务状态_已完成", &lRet);
    wd().FindStr(623, 251, 851, 461, L"完成", 0.95, &retx1, &rety1, &retx2, &rety2, &lRet);
    if (lRet != 0)
    {
        wd().UseDict(L"白色", &lRet);
        wd().FindStr(623, 251, 851, 461, L"交付人", 0.95, &retx1, &rety1, &retx2, &rety2, &lRet);
        if (lRet != 0)
        {

            wd().MoveTo(retx1 + 60, rety1, &lRet);
            wd().Delay(200);
            wd().LeftClick(&lRet);
            wd().Delay(200);
            wd().MoveTo(3, 3, &lRet);
            return TaskStatus_Running;
        }
    }
    // 蜀山杂货商  确定
    wd().FindPic(229, 180, 771, 509, L"确定.png", 0.95, &retx1, &rety1, &retx2, &rety2, &lRet);
    if (lRet != 0)
    {
        wd().MoveTo(retx1, rety1, &lRet);
        wd().Delay(200);
        wd().LeftClick(&lRet);
        return TaskStatus_Running;
    }
    // 装装备
    wd().UseDict(L"黄色标识", &lRet);
    wd().FindStr(95, 71, 874, 651, L"向下箭头", 0.95, &retx1, &rety1, &retx2, &rety2, &lRet);
    if (lRet != 0)
    {
        wd().MoveTo(retx1, rety1 + 50, &lRet);
        wd().Delay(400);
        wd().RightClick(&lRet);
        wd().Delay(200);
        wd().MoveTo(3, 3, &lRet);
        return TaskStatus_Running;
    }

    // 关闭 按钮
    wd().FindPic(95, 71, 874, 651, L"关闭.png", 0.95, &retx1, &rety1, &retx2, &rety2, &lRet);
    if (lRet != 0)
    {
        wd().MoveTo(retx1, rety1, &lRet);
        wd().Delay(200);
        wd().LeftClick(&lRet);
        return TaskStatus_Running;
    }
    //  需要打怪的任务 已完成
    wd().UseDict(L"任务个数_已完成", &lRet);
    wd().FindStr(623, 251, 851, 461, L"/", 0.95, &retx1, &rety1, &retx2, &rety2, &lRet);
    if (lRet != 0)
    {

        wd().UseDict(L"白色", &lRet);
        wd().FindStr(623, 251, 851, 461, L"交付人", 0.95, &retx1, &rety1, &retx2, &rety2, &lRet);
        if (lRet != 0)
        {

            wd().MoveTo(retx1 + 60, rety1, &lRet);
            wd().Delay(200);
            wd().LeftClick(&lRet);
            wd().Delay(200);
            wd().MoveTo(3, 3, &lRet);
            return TaskStatus_Running;
        }
    }

    static bool isMonsterTask = false;
    // 需要打怪的任务 采集的任务
    wd().UseDict(L"任务个数", &lRet);
    wd().FindStr(623, 251, 851, 461, L"/", 0.95, &retx1, &rety1, &retx2, &rety2, &lRet);
    if (lRet != 0)
    {
        if (isMonsterTask)
        {
            // 上等木料
            if (role_x_ == 103 && role_y_ == 142)
            {
                wd().MoveTo(598, 450, &lRet);
                wd().Delay(200);
                wd().LeftClick(&lRet);
                isMonsterTask = false;
            }
            else
                attackMonster();
        }
        else
        {
            wd().MoveTo(retx1 - 25, rety1, &lRet);
            wd().Delay(200);
            wd().LeftClick(&lRet);
            isMonsterTask = true;
        }
        return TaskStatus_Running;
    }
    else
    {
        isMonsterTask = false;
    }
    // 接受任务等
    wd().UseDict(L"白色", &lRet);
    do
    {
        wd().FindStr(133, 439, 313, 529, L"感叹号", 0.95, &retx1, &rety1, &retx2, &rety2, &lRet);
        if (lRet != 0)
        {
            wd().MoveTo(retx1 + 30, rety1, &lRet);
            wd().Delay(200);
            wd().LeftClick(&lRet);

            wd().Delay(600, 300);

            continue;
        }
    } while (false);
    return TaskStatus_Running;
}
